package com.itheima.health.service.impl;

import com.itheima.health.dao.RoleMenuMapper;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.RoleMenu;
import com.itheima.health.service.RoleMenuService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class RoleMenuServiceImpl implements RoleMenuService {
    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public void add(RoleMenu roleMenu) {
        roleMenuMapper.insert(roleMenu);
    }

    @Override
    public List<Integer> findmenuIdsById(Integer id) {
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setRoleId(id);
        List<RoleMenu> roleMenus = roleMenuMapper.select(roleMenu);
        List<Integer> select = new ArrayList<>();
        for (RoleMenu rolemenu : roleMenus) {
            Integer menuId = rolemenu.getMenuId();
            select.add(menuId);
        }
        return select;
    }

    @Override
    public void edit(Role role, Integer[] menuIds) {
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setRoleId(role.getId());
        roleMenuMapper.delete(roleMenu);
        for (Integer menuId : menuIds) {
            RoleMenu roleMenu1 = new RoleMenu();
            roleMenu1.setRoleId(role.getId());
            roleMenu1.setMenuId(menuId);
            roleMenuMapper.insert(roleMenu1);
        }
    }
}
